/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.ac.aptech.fat1.c0910g.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import vn.ac.aptech.fat1.c0910g.dao.FoodDAO;
import vn.ac.aptech.fat1.c0910g.dao.MenuDAO;
import vn.ac.aptech.fat1.c0910g.dao.impl.FoodDaoImpl;
import vn.ac.aptech.fat1.c0910g.dao.impl.MenuDaoImpl;
import vn.ac.aptech.fat1.c0910g.entity.Food;
import vn.ac.aptech.fat1.c0910g.entity.Menu;
import vn.ac.aptech.fat1.c0910g.util.DBUtility;

/**
 *
 * @author NDC
 */
public class DataServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            ServletContext context = getServletContext();
            Connection conn = DBUtility.getConnection(context);
            String view = request.getParameter("view");
            String action = request.getParameter("action");
            String strId = request.getParameter("id");
            int id = 0;
            try {
                id = Integer.parseInt(strId);
            } catch (Exception ex) {
                
            }
            if (view != null && view.trim().length() > 0) {
                if (view.equals("food")) {
                    FoodDAO foodDAO = new FoodDaoImpl(conn);
                    if (action.equals("list")) {
                        ArrayList<Food> foods = foodDAO.listFoods();
                        request.setAttribute("foods", foods);
                    } else if (action.equals("detail")) {                        
                        Food food = foodDAO.getFoodById(id);
                        request.setAttribute("food", food);
                    }
                }
                if (view.equals("menu")) {
                    MenuDAO menuDAO = new MenuDaoImpl(conn);
                    if (action.equals("list")) {
                        ArrayList<Menu> menus = menuDAO.listMenu();
                        for (Menu m : menus) {
                            m.setFoods(menuDAO.getMenuById(m.getId()).getFoods());
                        }
                        request.setAttribute("menus", menus);
                    } else if (action.equals("detail")) {
                        Menu menu = menuDAO.getMenuById(id);
                        request.setAttribute("menu", menu);
                    }
                }
            }
            DBUtility.closeConnection(conn);
        } catch (Exception ex) {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
